package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.FastDFSUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author wk
 * @create 2021-08-26 14:27
 */
@RequestMapping("/admin/product")
@RestController
@Api(tags = "文件上传")
public class FileController {

    @Value("${img.url}")
    private String imgUrl;

    // http://api.gmall.com/admin/product/fileUpload
    @PostMapping("/fileUpload")
    public Result fileUpload(MultipartFile file) {
        try {
            String fileId = FastDFSUtils.upload(file.getBytes(), file.getOriginalFilename());
            return Result.ok(imgUrl + fileId);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.fail();
    }
}
